﻿var manager = null;
var url = "../Org/TreeDate?rand="+Math.random();
$(function () {
    menuInit();
    InitTreeData();
});
 function InitTreeData() {
     $.get(url, {}, function (orgdata) {
         if (orgdata != undefined) {
             if (manager != null && manager != undefined) { manager.clear(); }
             manager = $(".l-tree").ligerTree({
                 data: [orgdata],
                 checkbox: false,
                 onClick: itemclick
             });
             $("#initLoadingImg").css("display", "none");

         }
     });
 }

 function itemclick(item, i) {
     $("#getnodeid").val(item.data.id);
     $("#getnodename").val(item.data.text);
     $("#getnodeisorg").val(item.data.isorg);
 }
 //新增
// function addBtnClickEvent(obj,type) {
//     var tag = false;
//     var id = $("#getnodeid").val();
//     var name = $("#getnodename").val();
//     var isorg = $("#getnodeisorg").val();
//     if (id == undefined || id == null || $.trim(id.toString()).length < 1) {
//         alert("请选择一个结点！");
//     } else {
//         var url = "../Org/Index?id=" + id.toString() + "&orgname=" + name.toString(); ;
//         if (type == "edit") { url = "../Org/Edit?id=" + id.toString() + "&orgname=" + name.toString(); }
//         else if (type == "adddept") { url = "../Dept/Index?id=" + id.toString() + "&orgname=" + name.toString() + "&isorg=" + (isorg.toString() == "1").toString(); }
//         else if (type == "delete") { url = "../Org/?id=" + id.toString(); }
//         //$(obj).attr("href", url).onclick
//         $(obj).attr("href", url).click();
//         tag = true;
//     }
//     return tag;
// }


 //首页 toolbar

 function menuInit() {
     $("#toptoolbar").ligerToolBar({ items: [
                { text: '添加组织机构', click: addOrgOperation, icon: 'add' },
                { line: true },
                 { text: '添加部门', click: addDeptOperation, icon: 'add' },
                { line: true },
                 { text: '修改', click: editOperation, icon: 'modify' },
                { line: true },
                { text: '删除', click: DeleteOperation, img: '../Content/images/delete.gif' }
            ]
     });
 }
 var id;
 var name;
 var isorg;
 function IsSelectOneNode()
 {
      id = $("#getnodeid").val();
      name = $("#getnodename").val();
      isorg = $("#getnodeisorg").val();
     var tipinfo="";
     if (id == undefined || id == null || $.trim(id.toString()).length < 1) {
         tipinfo="请选择一个结点！";
     } 
     if(tipinfo.length>0){
         $.ligerDialog.success(tipinfo); return false;
        }
        return true;
 }
 //
 //add
 function addOrgOperation() {
     if (IsSelectOneNode() == true) {
         if (isorg.toString() == "1") {addOrg(); }
         else { $.ligerDialog.success("请选择一个组织机构!"); return false; }
     }
 }
 function addDeptOperation() {
     if (IsSelectOneNode() == true) {
         addDept(); 
     }
 }
 function addOrg() {
     $.ligerDialog.open({ url: encodeURI("../Org/Index?id=" + id.toString() + "&orgname=" + name.toString()), height: getDialogHeight(), width: getDialogWidth(),
          isResize: true, isHidden: false
         });
 }
 function addDept() {
     $.ligerDialog.open({ url: encodeURI("../Dept/Add?id=" + id.toString() + "&orgname=" + name.toString() + "&isorg=" + (isorg.toString() == "1").toString()), height: getDialogHeight(), width: getDialogWidth()
         , isResize: true, isHidden: false
         });
 }
 //
 //
 function editOperation() {
     if (IsSelectOneNode() == true) {
         if (isorg.toString() == "1") { updateOrg(); }
         else {
             updateDept();
         }
     }
 }

 function updateOrg() {
     $.ligerDialog.open({ url: encodeURI("../Org/Edit?id=" + id.toString() + "&orgname=" + name.toString()), height: getDialogHeight(), width: getDialogWidth(),
         isResize: true, isHidden: false
         });
 }
 function updateDept() {
     $.ligerDialog.open({ url: encodeURI("../Dept/Edit?id=" + id.toString() + "&orgname=" + name.toString() + "&isorg=" + (isorg.toString() == "1").toString()), height: getDialogHeight(), width: getDialogWidth(),
          isResize: true, isHidden: false
         });
 }


 function DeleteOperation() {
     if (IsSelectOneNode() == true) {
         var deleteUrl = "";
         if (isorg.toString() == "1") {
             deleteUrl = "../Org/Delete";
          }
         else {
             deleteUrl = "../Dept/Delete";
         }
         $.get(deleteUrl, { "id": id.toString() }, function (data) {
             var tip = "";
             if (data != undefined && data == "ok") {
                 tip = "操作成功！";
                 InitTreeData();
             }
             else {
                 tip = "操作失败！";
             }
             $.ligerDialog.success(tip); return false;
         });
     }
 }